var PurchaseWarehouseIn = function () {
  var UpdateForm = function () {
    var template = /* html */ `
      <div class="update-form">
        <div>
          <el-date-picker style="width:100%" v-model="inDayTime" size="small" value-format="timestamp" :editable="false"
            :clearable="false" placeholder="请选择入库日期"></el-date-picker>
        </div>
        <div style="text-align:center;margin-top:5px">
          <el-button type="primary" size="small" @click="update">修改</el-button>
        </div>
      </div>
    `;

    return {
      props: {
        warehouses: {
          type: Array,
          required: true
        }
      },

      data: function () {
        return {
          inDayTime: null
        };
      },

      methods: {
        update: function () {
          var inDayTime = this.inDayTime;
          if (inDayTime === null) {
            this.$alert('请选择入库日期', '信息');
            return;
          }
          var warehouses = this.warehouses;
          for (var i = 0; i < warehouses.length; i++) {
            var warehouse = warehouses[i];
            warehouse.inDay = inDayTime;
          }
          this.$emit('update-success');
        }
      },

      template: template
    };
  }();

  var template = /*html*/ `
    <div class="purchase-warehousein" v-loading.fullscreen.lock="loading">
      <search-btn-view immediate btn-text="采购入库查询" @before-open="resetSearch" @search="searchData" v-if="providerNameObj!==null">
        <template slot="append">
          <el-button type="primary" size="medium" @click="saveWarehouseIn(false)">入库</el-button>
          <el-button type="primary" size="medium" @click="saveWarehouseIn(true)" :disabled="!selfApprovalPermission">审核过账</el-button>
          <el-button type="primary" size="medium" @click="mergeWarehouseIn">合并计算</el-button>

          <template v-if="selectWarehouseList.length>1">
            <el-popover v-model="openForm" title="修改入库日期" width="200" trigger="click" placement="bottom" style="margin-left:10px">
              <update-form v-if="openForm" :warehouses="selectWarehouseList" @update-success="openForm=false"></update-form>
              <el-button slot="reference" type="primary" size="medium">修改入库日期</el-button>
            </el-popover>
          </template>
        </template>
        <template slot="right">
          <table-col-show-select v-if="tableHideColumnKey!==null" :table-name="tableName" :current-column-name="currentColumnName" :remote-save="remoteSave"
            :hide-column.sync="tableHideColumnKey">
          </table-col-show-select>
        </template>
        <div slot="search-content">
          <sys-common-search request-url="materialSelectFilter/getMaterialSelectData" :request-data="{type:10}" :search="search"
            :search-field="searchField" label-width="auto" input-width="220px">
            <template slot="prepend">
              <el-form-item label="采购类型">
                <el-select clearable v-model="search.type" placeholder="请选择采购类型">
                  <el-option v-for="item in typeList" :key="'type_'+item.value" :value="item.value" :label="item.label" style="width:220px">
                  </el-option>
                </el-select>
              </el-form-item>
            </template>
            <template slot="append">
              <el-form-item label="采购单号">
                <el-input clearable placeholder="请输入采购单号" v-model="search.purchaseOrder" style="width:220px">
                </el-input>
              </el-form-item>
              <el-form-item label="采购数量">
                <el-input-number is-decimal-zero v-model="search.purchaseCount" placeholder="请输入采购数量" :controls="false" :min="0"
                  :precision="4" style="width:220px">
                </el-input-number>
              </el-form-item>
              <el-form-item label="采购日期">
                <el-date-picker style="width:135px" v-model="search.startDate" value-format="timestamp" :editable="false"
                  :picker-options="startDateOption"></el-date-picker>
                <span>-</span>
                <el-date-picker style="width:135px" v-model="search.endDate" value-format="timestamp" :editable="false"
                  :picker-options="endDateOption"></el-date-picker>
              </el-form-item>
              <el-form-item label="单价">
                <el-input-number is-decimal-zero v-model="search.startPrice" placeholder="单价最小值" :controls="false" :min="0" :max="search.endPrice"
                  :precision="2" style="width:110px"></el-input-number>
                <span>-</span>
                <el-input-number is-decimal-zero v-model="search.endPrice" placeholder="单价最大值" :controls="false" :precision="2"
                  :min="search.startPrice?search.startPrice:0" style="width:110px"></el-input-number>
              </el-form-item>
            </template>
          </sys-common-search>
        </div>
      </search-btn-view>
      <div style="margin-bottom:5px" class="purchase-warehousein-stat">
        <span>采购入库总数:{{totalPurchaseCount}}</span>
        <span>入库总金额:{{totalPurchaseMoney}}</span>
      </div>
      <div v-if="warehouseStatList.length>0" style="margin-bottom:5px" class="purchase-warehousein-stat">
        <template v-for="item in warehouseStatList">
        <span :key="'materialType_'+item.materialType">{{item.materialType}}:{{item.count}}</span>
        </template>
      </div>
      <div class="purchase-warehousein-table-div">
        <el-table fit stripe border ref="purchaseinWarehouseMaterial" :data="purchaseOrderData" highlight-current-row style="width:100%;" @selection-change="checkOrder" height="parent"
          :hide-column-keys="getTableHideColumns()">
          <el-table-column :resizable="false" sortable fixed prop="type" key="type" label="采购类型" min-width="90px" align="center">
            <template slot-scope="scope">
              <span>{{i18n.getMsg('purchaseInWarehouse.type.'+scope.row.type)}}</span>
            </template>
          </el-table-column>
          <el-table-column sortable fixed prop="purchaseId" key="purchaseId" label="采购单号" min-width="140px" header-align="center"></el-table-column>
          <el-table-column sortable fixed prop="companyOrderId" key="companyOrderId" label="订单编号" min-width="140px" header-align="center"></el-table-column>
          <el-table-column sortable fixed prop="factoryNo" key="factoryNo" label="工厂型号" width="120px" header-align="center"></el-table-column>
          <el-table-column sortable prop="coustomId" key="coustomId" label="客户型号" width="120px" header-align="center"></el-table-column>
          <el-table-column prop="providerName" key="providerName" label="供应商" min-width="160px" header-align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.providerList===null">{{scope.row.providerName}}</span>
              <table-select v-else filterable style="width:152px" v-model="scope.row.providerName" :options="scope.row.providerList" @change="providerNameChange(scope.row)">
              </table-select>
            </template>
          </el-table-column>
          <el-table-column sortable prop="materialName" key="materialName" label="物料名称" min-width="120px" header-align="center">
          </el-table-column>
          <el-table-column sortable prop="materialType" key="materialType" label="物料类型" min-width="120px" header-align="center">
          </el-table-column>
          <el-table-column sortable prop="color" key="color" label="编号" min-width="100px" header-align="center" header-align="center">
          </el-table-column>
          <el-table-column sortable sortable prop="colorDescription" key="colorDescription" label="描述" min-width="120px" header-align="center">
          </el-table-column>
          <el-table-column sortable prop="model" label="规格" key="model" min-width="120px" header-align="center"></el-table-column>
          <el-table-column sortable prop="materialDeptName" label="物料部门" key="materialDeptName" min-width="130px" align="center"></el-table-column>
          <el-table-column sortable prop="providerNo" label="供方编号" key="providerNo" min-width="120px" header-align="center"></el-table-column>
          <el-table-column sortable prop="unit" label="采购单位" key="unit" min-width="110px" align="center"></el-table-column>
          <el-table-column sortable prop="unitPrice" label="采购单价" key="unitPrice" min-width="110px" align="center"></el-table-column>
          <el-table-column sortable prop="purchaseCount" label="采购数量" key="purchaseCount" min-width="110px" align="center"></el-table-column>
          <el-table-column sortable prop="countb" label="已入数量" key="countb" min-width="110px" align="center"></el-table-column>
          <el-table-column :resizable="false" label="入库单位" key="inCountUnit" width="100px" header-align="center">
            <template slot-scope="scope">
              <el-select clearable v-model="scope.row.inCountUnit" filterable style="width:92px" placeholder="请选择" size="mini">
                <el-option v-for="item in unitList" :key="item" :label="item" :value="item">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="入库单价" key="inCountPrice" width="100px" align="center">
            <template slot-scope="scope">
              <el-input-number v-if="canUpdatePrice" v-model="scope.row.inCountPrice" style="width:92px" :precision="4"
                :min="scope.row.inCountPriceMin" :max="scope.row.inCountPriceMax" :controls="false" size="mini" @change="unitPriceChange(scope.row)">
              </el-input-number>
              <span v-else>{{scope.row.inCountPrice}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="采购入库数量" key="count" width="100px" header-align="center">
            <template slot-scope="scope">
              <el-input-number is-decimal-zero v-model="scope.row.count" style="width:92px" :min="0" :max="scope.row.purchaseCountMax" :controls="false" size="mini"
                :precision="SysUtil.getMaterialNumberPrecision(scope.row.unit,4)" @change="unitPriceChange(scope.row)">
              </el-input-number>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="报废数量" key="scrapCount" width="100px" header-align="center">
            <template slot-scope="scope">
              <el-input-number is-decimal-zero v-model="scope.row.scrapCount" style="width:92px" :min="0" :controls="false" size="mini"
                :precision="SysUtil.getMaterialNumberPrecision(scope.row.unit,4)">
              </el-input-number>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="入库结算数量" key="sumMoneyCount" width="100px" header-align="center">
            <template slot-scope="scope">
              <el-input-number is-decimal-zero v-model="scope.row.sumMoneyCount" style="width:92px" :min="0" :controls="false" size="mini"
                :precision="SysUtil.getMaterialNumberPrecision(scope.row.inCountUnit,4)" @change="unitPriceChange(scope.row)"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="金额" key="inOutMoney" width="140px" align="center">
            <template slot-scope="scope">
              <el-input-number v-if="canUpdatePrice" v-model="scope.row.inOutMoney" style="width:130px" :min="0" :controls="false"
                :precision="2" size="mini" :after-change-format-value="function(value){return formatInOutMoney(value,scope.row)}"
                @change="totalMoneyChange(scope.row)"></el-input-number>
              <span v-else>{{scope.row.inOutMoney}}</span>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="是否含税" key="isTax" width="140px" align="center">
            <template slot-scope="scope">
              <el-select v-model="scope.row.isTax" size="mini" style="width:130px">
                <el-option :value="1" label="不含税"></el-option>
                <el-option :value="2" label="含税"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="仓位" key="positions" width="180px" key="positions" header-align="center">
            <template slot-scope="scope">
              <sys-autocomplete open-all-dropdown :key="scope.row._dataId" v-model="scope.row.positions" size="mini" style="width:170px"
                :options="scope.row.positionList"></sys-autocomplete>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="入库日期" key="inDay" width="135px" key="inDay" header-align="center">
            <template slot-scope="scope">
              <el-date-picker style="width:125px" v-model="scope.row.inDay" size="mini" value-format="timestamp" :editable="false"
                :clearable="false"></el-date-picker>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="备注" key="remark" width="130px" key="remark" header-align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" size="mini" style="width:120px"></el-input>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" :selectable="isChecked" type="selection" key="selection" width="40px" align="center" fixed="right">
          </el-table-column>
        </el-table>
      </div>
      <table-pagination v-if="pagination.totalCount!==0" :pagination="pagination" @change="pageChange"></table-pagination>

      <el-dialog title="合并计算" :visible.sync="openDialog" top="calc(50vh - 85px)" width="250px">
        <div style="height:110px;overflow:auto">
          <merge-purchase v-if="openDialog" :warehouses="selectWarehouseList" @merge-success="mergeSuccess"></merge-purchase>
        </div>
      </el-dialog>
    </div>
  `;
  return {
    mixins: [
      tablePaginationMixin(),
      datePickerOptionsMixin('startDate', 'endDate'),
      tableColShowMixin('purchaseinWarehouseMaterial')
    ],
    props: {
      unitList: {
        type: Array,
        required: true
      }
    },
    computed: {
      totalPurchaseMoney: function () {
        var warehouseList = this.selectWarehouseList;
        var totalMoney = _.sumBy(warehouseList, function (warehouse) {
          var inOutMoney = warehouse.inOutMoney;
          if (inOutMoney === undefined) {
            inOutMoney = 0;
          }
          return inOutMoney;
        });
        totalMoney = _.round(totalMoney, 2);
        return totalMoney;
      },
      totalPurchaseCount: function () {
        var selectWarehouseList = this.selectWarehouseList;
        var totalCount = _.sumBy(selectWarehouseList, function (warehouse) {
          var count = warehouse.count;
          if (count === undefined) {
            count = 0;
          }
          return count;
        });
        totalCount = _.round(totalCount, 4);
        return totalCount;
      },
      warehouseStatList: function () {
        var warehouseList = this.selectWarehouseList;
        var warehouseGroup = _.groupBy(warehouseList, function(obj){
          var materialType = obj.materialType;
          if (SysUtil.isEmpty(materialType)) {
            return '无';
          }
          return materialType;
        });
        var statList = [];
        for (var materialType in warehouseGroup) {
          var list = warehouseGroup[materialType];
          var totalCount = _.sumBy(list, function (obj) {
            var count = obj.count;
            if (SysUtil.isEmpty(count)) {
              return 0;
            }
            return count;
          });
          statList.push({
            materialType: materialType,
            count: totalCount
          });
        }
        return statList;
      }
    },
    data: function () {
      var search = {
        purchaseOrder: '',
        materialName: '',
        materialType: '',
        model: '',
        color: '',
        providerName: '',
        materialDept: '',
        providerNo: '',
        orderId: '',
        factoryNo: '',
        customerNo: '',
        companyOrderId: '',
        startDate: null,
        endDate: null,
        type: '',
        purchaseCount: undefined,
        startPrice: undefined,
        endPrice: undefined
      };
      return {
        loading: false,
        moment: moment,
        selfApprovalPermission: false,
        purchaseOrderData: [],
        canUpdateUnitAndPrice: true,
        selectWarehouseList: [],
        search: _.cloneDeep(search),
        oldSearch: _.cloneDeep(search),
        typeList: i18n.getDropDown('purchaseInWarehouse.type', [1, 2]),
        searchField: ['materialName', 'materialType', 'model', 'color', 'providerName', 'providerNo', 'materialDept', 'orderId', 'companyOrderId',
          'factoryNo', 'customerNo'
        ],
        canUpdatePrice: false,
        openDialog: false,
        openForm: false,
        providerNameObj: null
      };
    },
    created: function () {
      this.loading = true;
      var self = this;
      return Promise.all([
        this.setUpdateValid(),
        this.checkApprovalPermission(),
        this.setProviderNameList()
      ]).then(function () {
        self.loading = false;
      }).catch(function () {
        self.loading = false;
      });
    },
    methods: {
      setUpdateValid: function () {
        var self = this;
        return SysUtil.getUser().then(function (user) {
          var canUpdatePrice = user.companyInfo.canUpdatePriceAndUnit === 1;
          self.canUpdatePrice = canUpdatePrice;
          return canUpdatePrice;
        });
      },

      checkApprovalPermission: function () {
        var self = this;
        return PermissionUtil.ajaxCheckPermission(6).then(function (hasApproval) {
          self.selfApprovalPermission = hasApproval;
          return hasApproval;
        });
      },

      setProviderNameList: function () {
        var self = this;
        return ajaxRequest('basicData/getCustomerName').then(function (providerList) {
          var providerNameObj = {};
          for (var i = 0; i < providerList.length; i++) {
            var provider = providerList[i];
            var isTax = provider.isTax;
            if (isTax === 0) {
              isTax = 2;
            }
            providerNameObj[provider.customerName] = isTax;
          }
          self.providerNameObj = providerNameObj;
          return providerNameObj;
        });
      },

      resetSearch: function () {
        this.search = _.cloneDeep(this.oldSearch);
      },

      searchData: function () {
        this.setPurchaseWarehouseInData(true);
      },

      pageChange: function () {
        this.setPurchaseWarehouseInData();
      },

      setPurchaseWarehouseInData: function (isSearch) {
        if (!isSearch) {
          this.search = _.cloneDeep(this.oldSearch);
        }
        var search = this.search;
        var pagination = this.pagination;
        var currentPage = pagination.currentPage;
        var pageSize = pagination.pageSize;
        var start = (currentPage - 1) * pageSize;
        var postData = {
          start: start,
          length: pageSize
        }
        var fields = ['customerNo', 'orderId', 'companyOrderId', 'purchaseOrder', 'factoryNo', 'materialName', 'model', 'color', 'providerName',
          'providerNo', 'type', 'purchaseCount', 'materialType', 'startPrice', 'endPrice'
        ];
        SysUtil.setPostData(postData, search, fields);

        var materialDept = search.materialDept;
        if (materialDept !== '') {
          postData.materialDeptId = materialDept.materialDeptId;
        }

        var startDate = search.startDate;
        if (startDate !== null) {
          postData.purchaseStartTime = moment(startDate).format('YYYY-MM-DD');
        }

        var endDate = search.endDate;
        if (endDate !== null) {
          postData.purchaseEndTime = moment(endDate).format('YYYY-MM-DD');
        }

        this.loading = true;
        var self = this;
        ajaxRequest("purchaseOrder/getPurchaseOrderOnPurchaseWarehouseIn", postData, false).then(function (res) {
          var dataList = res.returnData;
          if (dataList === undefined) {
            var info = res.info;
            if (info) {
              self.$alert(info, '信息');
              self.loading = false;
              return;
            }
          }
          if (isSearch) {
            self.oldSearch = _.cloneDeep(self.search);
          }
          var unitList = self.unitList;
          var providerNameObj = self.providerNameObj;
          for (var i = 0; i < dataList.length; i++) {
            var data = dataList[i];
            PurchaseWarehouseInUnit.setInCountUnit(data, unitList);

            var count = data.count,
              unitPrice = data.unitPrice;
            var adJustInPrice = data.adJustInPrice;
            if (adJustInPrice !== 0) {
              var inCountPriceMin = _.round(unitPrice - adJustInPrice, 4);
              if (inCountPriceMin < 0) {
                inCountPriceMin = 0;
              }
              data.inCountPriceMin = inCountPriceMin;

              var inCountPriceMax = _.round(unitPrice + adJustInPrice, 4);
              data.inCountPriceMax = inCountPriceMax;
            } else {
              data.inCountPriceMin = 0;
              data.inCountPriceMax = Infinity;
            }

            data.purchaseCountMax = count + data.adJustInCount;
            data.scrapCount = 0;
            data.sumMoneyCount = count;
            data.inCountPrice = unitPrice;
            data.inOutMoney = _.round(count * unitPrice, 2);
            var inDay = moment().valueOf();
            if (SysUtil.isEmpty(inDay)) {
              inDay = (new Date).getTime();
            }
            data.inDay = inDay;

            var positionList = data.positionList;
            var positions = '';
            if (!SysUtil.isEmpty(positionList)) {
              positions = positionList[0];
            }
            data.positions = positions;

            var subProviderSet = data.subProviderSet;
            var providerList = null;
            if (!SysUtil.isEmpty(subProviderSet)) {
              var providerName = data.providerName;
              var providerList = [providerName];
              providerList.pushArray(subProviderSet);
            }
            data.providerList = providerList;

            var providerName = data.providerName;
            var isTax = providerNameObj[providerName];
            if (isTax === undefined) {
              isTax = '';
            }
            data.isTax = isTax;

            SysUtil.tableSetDeptData(data);
          }
          self.purchaseOrderData = dataList;
          self.pagination.totalCount = res.iTotalRecords;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      saveWarehouseIn: function (isAduit) {
        var warehouseList = this.selectWarehouseList;
        var jsonArr = [];
        if (warehouseList.length <= 0) {
          this.$alert('请选择需要入库的订单', '信息');
          return;
        }

        var fields = ['purchaseId', 'type', 'purchaseDetailId', 'materialCode', 'count', 'positions', 'remark', 'colorDescription',
          'sumMoneyCount', 'providerName', 'inCountUnit', 'orderId', 'materialType', 'rowIndex', 'inCountPrice', 'inOutMoney', 'isTax'
        ];
        for (var i = 0; i < warehouseList.length; i++) {
          var warehouse = warehouseList[i];
          var code = this.checkValid(warehouse);
          if (code !== '') {
            var msg = i18n.getMsg(`purchaseInWarehouse.error.${code}`);
            this.$alert(msg, '信息');
            return;
          }
          var postData = {
            warehouseType: 4,
          };
          SysUtil.setPostData(postData, warehouse, fields, false, true);

          var scrapCount = warehouse.scrapCount;
          if (scrapCount === undefined) {
            scrapCount = 0;
          }
          postData.scrapCount = scrapCount;

          postData.inDay = moment(warehouse.inDay).format('YYYY-MM-DD');
          jsonArr.push(postData);
        }

        this.loading = true;
        var self = this;
        ajaxRequest("warehouse/saveWarehouseInByPurchaseOrder", {
          array: JSON.stringify(jsonArr),
          isAduit: isAduit
        }).then(function (res) {
          self.$alert(res, '信息');
          self.loading = false;
          self.setPurchaseWarehouseInData(true);
        }).catch(function () {
          self.loading = false;
        });
      },

      checkValid: function (warehouse) {
        var fields = ['inCountUnit', 'inCountPrice', 'count', 'sumMoneyCount', 'inOutMoney'];
        var zeroFields = ['count', 'sumMoneyCount'];
        for (var i = 0; i < fields.length; i++) {
          var field = fields[i];
          var value = warehouse[field];
          if (SysUtil.isEmpty(value)) {
            return field;
          }

          if (zeroFields.indexOf(field) === -1) {
            continue;
          }
          if (value === 0) {
            return field + 'IsZero';
          }
        }

        var scrapCount = warehouse.scrapCount;
        var count = warehouse.count;
        if (scrapCount !== undefined) {
          count += scrapCount;
          count = _.round(count, 4);
        }

        if (count > warehouse.purchaseCountMax) {
          return 'count>purchaseCountMax';
        }

        return '';
      },

      checkOrder: function (selection) {
        this.selectWarehouseList = selection;
      },

      isChecked: function (row) {
        return row.isAdvancePurchase === 0 && row.type !== 3;
      },

      unitPriceChange: function (warehouse) {
        var count = warehouse.sumMoneyCount;
        if (count === 0 || count === undefined) {
          count = warehouse.count;
        }
        var unitPrice = warehouse.inCountPrice;
        var totalMoney = SysUtil.calPrice(count, unitPrice, 'total');
        warehouse.inOutMoney = totalMoney;
      },

      totalMoneyChange: function (warehouse) {
        var count = warehouse.sumMoneyCount;
        if (count === 0 || count === undefined) {
          count = warehouse.count;
        }
        var totalMoney = warehouse.inOutMoney;
        var unitPrice = SysUtil.calPrice(count, totalMoney, 'unit', 4);
        warehouse.inCountPrice = unitPrice;
      },

      formatInOutMoney: function (value, warehouse) {
        var count = warehouse.sumMoneyCount;
        if (count === 0 || count === undefined) {
          count = warehouse.count;
        }
        var unitPrice = SysUtil.calPrice(count, value, 'unit', 4);
        var adJustPrice = unitPrice;
        var inCountPriceMin = warehouse.inCountPriceMin;
        var inCountPriceMax = warehouse.inCountPriceMax;
        if (unitPrice < inCountPriceMin) {
          adJustPrice = inCountPriceMin;
        } else if (unitPrice > inCountPriceMax) {
          adJustPrice = inCountPriceMax;
        }

        if (adJustPrice === unitPrice) {
          return true;
        }

        var totalMoney = SysUtil.calPrice(count, adJustPrice, 'total');
        return totalMoney;
      },

      mergeWarehouseIn: function () {
        var warehouseList = this.selectWarehouseList;
        if (warehouseList.length === 0) {
          this.$alert('请选择需要合并计算的采购单', '信息');
          return;
        }

        for (var i = 0; i < warehouseList.length; i++) {
          var warehouse = warehouseList[i];
          var code = this.checkValid(warehouse);
          if (code !== '') {
            var msg = i18n.getMsg(`purchaseInWarehouse.error.${code}`);
            this.$alert(msg, '信息');
            return;
          }
        }

        warehouseList = _.uniqBy(warehouseList, 'inCountUnit');
        if (warehouseList.length > 1) {
          this.$alert('所选择的采购单的物料入库单位不一致，不能合并计算', '信息');
          return;
        }
        this.openDialog = true;
      },

      mergeSuccess: function () {
        this.openDialog = false;
      },

      providerNameChange: function (purchase) {
        var providerName = purchase.providerName;
        var providerNameObj = this.providerNameObj;
        var isTax = providerNameObj[providerName];
        if (isTax === undefined) {
          isTax = 2;
        }
        purchase.isTax = isTax;
      }
    },
    components: {
      SearchBtnView: SearchBtnView,
      SysCommonSearch: SysCommonSearch,
      SysAutocomplete: SysAutocomplete,
      TableSelect: TableSelect,
      MergePurchase: MergePurchase,
      UpdateForm: UpdateForm
    },
    template: template
  }
}()
